﻿
using Newtonsoft.Json;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Controllers
{
    public class SysCodeController : Controller
    {
        private SysCodeServers syscodeServers;

        public SysCodeController()
        {
            syscodeServers = new SysCodeServers();
        }


        // GET: System/SysCode
        public ActionResult Index()
        {
            var PARENT_NOData = syscodeServers.GetPARENT_NO();
            PARENT_NOData.Insert(0, new SelectListItem { Value = "", Text = "全分类" });
            ViewBag.PARENT_NOData = PARENT_NOData;
            ViewBag.PARENTData = JsonConvert.SerializeObject(syscodeServers.GetPARENT_NO());
            return View();
        }

        // GET: System/SysCode/Edit/5
        public ActionResult Edit(int? id)
        {
            ViewBag.PARENT_NOData = syscodeServers.GetPARENT_NO();
            var model = new sys_code();
            if (id != null)
            {
                ViewBag.Title = "编辑系统代码";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建系统代码";
            }
            return View(model);
        }

        // POST: System/SysCode/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, sys_code model)
        {
            ViewBag.PARENT_NOData = syscodeServers.GetPARENT_NO();
            try
            {
                if (id != null)
                {
                    syscodeServers.Update(model);
                    ViewBag.msg = "修改系统代码信息成功！";
                }
                else
                {

                    syscodeServers.Add(model);
                    ViewBag.msg = "新增系统代码信息成功！";
                    model = new sys_code();
                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: System/SysCode/Delete/5
        public ActionResult Delete(int id)
        {
            if (id != 0)
            {
                syscodeServers.Del(id.ToString());
                ViewBag.msg = "删除系统代码信息成功！";
            }
            return Content("");
        }


        #region //GetData
        public string GetTableJsonResult(string CODE_NO,string CODE_NAME,string PARENT_NO)
        {
            var list = syscodeServers.GetData(CODE_NO, CODE_NAME, PARENT_NO);
            return JsonConvert.SerializeObject(list);
        }

        public sys_code GetModel(string id)
        {
            var model = syscodeServers.GetModelById(id);
            return model;
        }

        #endregion //GetData
    }
}